import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/widget/group_text_filed.dart';
import 'package:image_pickers/image_pickers.dart';

class CommentFootView extends StatefulWidget {
  final VideoModel? model;
  final Function(bool) begainCommentHandler; // true 发送
  final bool isSendStatus;
  final TextEditingController textEditController;
  final String? imagePath;
  final Function(String?)? imgCallback;
  const CommentFootView({
    super.key,
    this.model,
    required this.begainCommentHandler,
    this.isSendStatus = false,
    required this.textEditController,
    this.imagePath,
    this.imgCallback,
  });

  @override
  State<CommentFootView> createState() => _CommentFootViewState();
}

class _CommentFootViewState extends State<CommentFootView> {
  bool isImageSelectIng = false;
  void choseImg() async {
    isImageSelectIng = true;
    final listMedia = await ImagePickers.pickerPaths(
      uiConfig: UIConfig(uiThemeColor: Colors.white),
      galleryMode: GalleryMode.image,
      selectCount: 1,
      showCamera: false,
    );
    if (listMedia.isNotEmpty) {
      final ret = listMedia.map((e) => e.path ?? '').toList();
      String selectedImgPath = ret.first;
      widget.imgCallback?.call(selectedImgPath);
    }
    setState(() {});
    Future.delayed(Duration(milliseconds: 500), () {
      isImageSelectIng = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 58,
      color: Colors.black,
      padding: EdgeInsets.symmetric(horizontal: 18.w),
      alignment: Alignment.centerLeft,
      child: Row(
        children: [
          Expanded(
            child: GestureDetector(
              onTap: () => widget.begainCommentHandler(false),
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 2),
                child: GroupTextFiled(
                  controller: widget.textEditController,
                  height: 32,
                  enabled: false,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  textStyle: TextStyle(color: Colors.white, fontSize: 14),
                  placeholder: '请在此输入评论',
                  placeholderTextStyle: TextStyle(color: Color(0xff4D4D4D), fontSize: 14),
                  decoration: BoxDecoration(color: Color(0xff1A1A1A), borderRadius: BorderRadius.circular(40)),
                ),
              ),
            ),
          ),
          12.sizeBoxW,
          Stack(
            children: [
              Container(
                padding: EdgeInsets.only(right: 12, top: 4, bottom: 4),
                child: GestureDetector(
                  onTap: choseImg,
                  child: widget.imagePath?.isNotEmpty == true
                      ? Image.file(
                    File(widget.imagePath!),
                    width: 30,
                    height: 30,
                    fit: BoxFit.cover,
                  )
                      : Image.asset(
                    'icon_pic.webp'.commentPath,
                    width: 30,
                    height: 30,
                  ),
                ),
              ),
              if (widget.imagePath?.isNotEmpty == true)
                Positioned(
                  top: 0,
                  right: 6,
                  child: GestureDetector(
                    onTap: () {
                      widget.imgCallback?.call(null);
                      setState(() {});
                    },
                    child: Image.asset(
                      "close_grey.png".commonImgPath,
                      width: 12,
                      height: 12,
                    ),
                  ),
                ),
            ],
          ),
          Container(
            width: 30,
            height: 30,
            child: GestureDetector(
              onTap: () => widget.begainCommentHandler(true),
              child: widget.isSendStatus
                  ? CupertinoActivityIndicator(
                color: AppColors.actionRed,
                radius: 12,
              )
                  : Image.asset('ic_send.webp'.commentPath),
            ),
          )
        ],
      ),
    );
  }
}